﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class LogObject
    {
        public static Log[] GetLogs()
        {
            return Query(null);
        }

        public Log GetLog(int id)
        {
            Log log = new Log();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(LogDesc.Id,     ConditionExpress.EQUALS, id);
            builder.QueryTableName = LogDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable logs = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            log.Id                   = Convert.ToInt32(logs.Rows[0][LogDesc.Id]);
            log.EmployeeId           = Convert.ToInt32(logs.Rows[0][LogDesc.EmployeeId]);
            log.Time                 = Convert.ToDateTime(logs.Rows[0][LogDesc.Time]);
            log.Description          = Convert.ToString(logs.Rows[0][LogDesc.Description]);

            return log;
        }

        public static Log[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + LogDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                Log[] logs = new Log[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Log log = new Log();

                    log.Id              = Convert.ToInt32(table.Rows[i][LogDesc.Id]);
                    log.EmployeeId      = Convert.ToInt32(table.Rows[i][LogDesc.EmployeeId]);
                    log.Time            = Convert.ToDateTime(table.Rows[i][LogDesc.Time]);
                    log.Description     = Convert.ToString(table.Rows[i][LogDesc.Description]);


                    logs.SetValue(log, i);
                }

                return logs;
            }
            return null;
        }

        public Int32 Add(Log log)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(LogDesc.EmployeeId,              log.EmployeeId);
            builder.InsertAdd(LogDesc.Time,                    log.Time);
            builder.InsertAdd(LogDesc.Description,             log.Description);

            builder.QueryTableName = LogDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(LogDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = LogDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public Int32 Update(Log log)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(LogDesc.EmployeeId,             log.EmployeeId);
            builder.UpdateAdd(LogDesc.Time,                   log.Time);
            builder.UpdateAdd(LogDesc.Description,            log.Description);

            builder.ConditionAdd(LogDesc.Id, ConditionExpress.EQUALS, log.Id);
            builder.QueryTableName = LogDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
